package com.example.lte;

import cn.dev33.satoken.SaTokenManager;
import cn.dev33.satoken.spring.SaTokenSetup;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableRetry //Spring重试注解
@SaTokenSetup  //sa-token启用注解
@EnableScheduling //spring定时任务启用注解
@SpringBootApplication
@NacosPropertySource(dataId = "lteAdminConfig", autoRefreshed = true)
public class LteApplication implements CommandLineRunner {


    //使用Nacos配置中心上设置好的lteAdminConfig配置
    @NacosValue(value = "${counter:0}", autoRefreshed = true)
    public Long counter;

    @GetMapping(path = "/get")
    public String get() {
        return String.format("Counter value:%d", counter);
    }


    /**-------------- 通过Naming服务注册实例到注册中心---start---------------------------------------*/
    @NacosInjected
    private NamingService namingService;

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${server.port}")
    private Integer serverPort;

    @Override
    public void run(String... args) throws Exception {
        // 通过Naming服务注册实例到注册中心
        namingService.registerInstance(applicationName, "127.0.0.1", serverPort);
    }

    /**-------------- 通过Naming服务注册实例到注册中心---end---------------------------------------*/




    public static void main(String[] args) {
        SpringApplication.run(LteApplication.class, args);
        System.out.println("启动成功：sa-token配置如下：" + SaTokenManager.getConfig());
    }


}
